/*
 * @Author: your name
 * @Date: 2020-09-18 11:36:06
 * @LastEditTime: 2020-12-25 14:40:17
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \egg-RESTfulAPI\app\controller\virtualEtc.js
 */
/*
 * @Author: your name
 * @Date: 2020-09-18 11:36:06
 * @LastEditTime: 2020-09-18 15:18:21
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \egg-RESTfulAPI\app\controller\virtualEtc.js
 */
const { v4 } = require('uuid')
const Controller = require('egg').Controller
let moment = require('moment');
class ETCController extends Controller {
    constructor(ctx) {
        super(ctx)


    }



    async getAllScene() {
        const { ctx } = this
        let scenes = await this.ctx.model.Scene.find()
        ctx.helper.success({ ctx, res: { data: scenes } })
    }



    async deleteScene() {
        const { ctx } = this
        let { sceneName } = ctx.request.body
        await this.ctx.model.Scene.remove({ sceneName })
        let scenes = await this.ctx.model.Scene.find()
        ctx.helper.success({ ctx, res: { data: scenes } })
    }
    async getScene() {
        const { ctx } = this
        let { sceneName } = ctx.request.body
        let scene = await this.ctx.model.Scene.findOne({ sceneName })
        ctx.helper.success({ ctx, res: { data: scene } })
    }
    async saveScene_step1() { //保存场景1
        const { ctx } = this
        let { sceneName } = ctx.request.body
        let scene = await this.ctx.model.Scene.findOneAndUpdate({ sceneName }, { sceneName, status: 0 }, { 'upsert': true, new: true })

        ctx.helper.success({ ctx, res: { data: scene } })
    }
    async saveScene_step2() { //保存场景2
        const { ctx } = this
        let { sceneName, numberOfSimulatedVehicles, beginTime, endTime } = ctx.request.body
        let scenes = await this.ctx.model.Scene.findOne({ sceneName })

        if (!scenes || scenes.status >= 2) {
            ctx.helper.InvaidRequest({ ctx, msg: "请先创建场景" })
            return
        }

        let scene = await this.ctx.model.Scene.findOneAndUpdate({ sceneName }, { sceneName, numberOfSimulatedVehicles, endTime, beginTime, status: 1 }, { 'upsert': true, new: true })

        ctx.helper.success({ ctx, res: { data: scene } })
    }
    async getSceneRoad() { //保存场景2
        const { ctx } = this
        let { scene } = ctx.request.body
        let sceneRoads = await this.ctx.model.SceneBaseRoad.find({ scene })
        // sceneRoads = JSON.parse(JSON.stringify(sceneRoads))
        sceneRoads.forEach((_, index) => {

            sceneRoads[index].geometry = JSON.parse(sceneRoads[index].geometry)
        })

        ctx.helper.success({ ctx, res: { data: sceneRoads } })
    }

    async sceneAddEtcStation() {
        const { ctx } = this
        let { scene, roadId, lon, lat, stationName1, stationName2, stationType } = ctx.request.body
        let sceneRoad = await this.ctx.model.SceneBaseRoad.findOne({ scene, road_Id: roadId })
        if (!sceneRoad) {
            ctx.helper.InvaidRequest({ ctx, msg: "道路id异常" })
            return
        }

        let etcStation = await this.ctx.model.SceneEtcStation.create(
            [
                {
                    scene, roadId, lon, lat,
                    stationName1, stationName2, stationType,
                    belongRoadId: sceneRoad.Id,

                }
            ]
        )
        // let roadEtcStation = await this.ctx.model.SceneEtcStation.find({ scene, roadId })
        ctx.helper.success({ ctx, res: { data: etcStation } })
    }

    async sceneEtcStationModifyProperty() {
        const { ctx } = this
        let { stationName1, stationName2, stationType, lon, lat, etcStationId } = ctx.request.body
        let roadEtcStation = await this.ctx.model.SceneEtcStation.findOneAndUpdate({ _id: etcStationId }, { stationName1, lon, lat, stationName2, stationType }, { 'upsert': true, new: true })

        ctx.helper.success({ ctx, res: { data: roadEtcStation } })
    }

    async deleteSceneEtcStation() {
        const { ctx } = this
        let { etcStationId } = ctx.request.body

        let etcStation = await this.ctx.model.SceneEtcStation.findOne({ _id: etcStationId })
        if (!etcStation) {
            ctx.helper.InvaidRequest({ ctx, msg: "门架ID异常" })
            return
        }
        await this.ctx.model.SceneEtcStation.remove({ pairId: etcStation.pairId })

        let roadEtcStation = await this.ctx.model.SceneEtcStation.find({ scene: etcStation.scene, roadId: etcStation.roadId })

        ctx.helper.success({ ctx, res: { data: roadEtcStation } })
    }

    async getSceneEtcStation() {
        const { ctx } = this


        let SceneEtcStationSchema = await this.ctx.model.SceneEtcStation.find({})

        ctx.helper.success({ ctx, res: { data: SceneEtcStationSchema } })
    }




}


module.exports = ETCController